#include <assert.h>
#include <math.h>
#include <stdio.h>
#include <stdlib.h>
#include <time.h>

#include "dominion.h"
#include "dominion_helpers.h"
#include "rngs.h"
#include "testutils.h"

void treasure_map_test()
{
  int i, j, pass, c, tempCard, coin_bonus, gold;
  struct gameState state;

  for(i = 0; i < MAX_ITERATIONS; i++){
    coin_bonus = 0;
    pass = 0;
    init_test_data(&state, 1);
    
    srand(time(NULL));
    
    state.deckCount[0] = 50;
    for(j = 0; j < state.deckCount[0]; j++){
      state.deck[0][j] = random_card();
    }
    printf("ITERATION %d\n", i);
    switch(c = i % 2)
      {
      case 0:
	// test when only one treasure map is in hand
	state.handCount[0] = 5;
	for(j = 0; j < 4; j++){
	  // make sure no treasure maps are in hand
	  while((tempCard = random_card()) != treasure_map){
	    state.hand[0][j] = tempCard;
	    break;
	  }
	}
	// add a single treasure map to hand
	state.hand[0][4] = treasure_map;
	printf("-- One Treasure Map in hand --\n");

	gold = card_count(state.hand[0], gold, state.handCount[0]);

	if(cardEffect(state.hand[0][4], -1, -1, -1, &state, 4, &coin_bonus) != -1)
	  printf("Failure, card effect did not return correctly\n");
	else
	  pass++;
	
	if(state.discard[0][0] == treasure_map)
	  printf("Failure, card discarded when only one card in hand\n");
	else
	  pass++;
	 
	if(state.hand[0][4] != treasure_map)
	  printf("Failure, card removed from hand when only one card in hand\n");
	else
	  pass++;	
	
	if(card_count(state.hand[0], gold, state.handCount[0]) > gold)
	  printf("Failure, gold cards gained\n");
	else
	  pass++;

	if(pass == 4)
	  printf("Success, no cards removed or discarded and function returned correctly\n");
	
	break;
      case 1:
	// test when two treasure maps are in hand
	state.handCount[0] = 5;
        for(j = 0; j < 3; j++){
          // make sure no treasure maps are in hand
          while((tempCard = random_card()) != treasure_map){
            state.hand[0][j] = tempCard;
            break;
          }
        }
        // add two treasure map to hand
        state.hand[0][3] = treasure_map;
	state.hand[0][4] = treasure_map;
        printf("-- Two Treasure Map in hand --\n");
	
	gold = card_count(state.hand[0], gold, state.handCount[0]);

        if(cardEffect(state.hand[0][4], -1, -1, -1, &state, 4, &coin_bonus) == -1)
          printf("Failure, card effect did not return correctly\n");
        else
          pass++;

        if(state.discard[0][0] != treasure_map)
          printf("Failure, card not discarded\n");
        else
          pass++;

        if(state.hand[0][4] == treasure_map)
          printf("Failure, card still in hand\n");
        else
          pass++;
	
	if(card_count(state.hand[0], gold, state.handCount[0]) != (gold + 4))
          printf("Failure, 4 gold cards not gained\n");
        else
          pass++;
	
        if(pass == 4)
          printf("Success, no cards removed or discarded and function returned correctly\n");
	
	break;
      }
  }
}

int main()
{
  treasure_map_test();
  return 0;
}
